<?php

	define('OBJECT', 'OBJECT', true);
	define('OBJECT_K', 'OBJECT_K', false);
	define('ARRAY_A', 'ARRAY_A', false);
	define('ARRAY_N', 'ARRAY_N', false);
	
	eb_include_interface("IDataSource");

	class EbMySQLDataSource implements IDataSource
	{
		var $source;
		var $connection;
		var $outputFormat;
		var $rowAffected;
		var $result;
		var $connected = false;
		var $tryConnect = false;
		
		public function __construct($source)
		{
			if (is_array($source))
			{		
				$this->source = $source;
			}
			else
			{
				$this->source = eb_parse_parameters_string($source);
			}
		}
	
		function connect($outputFormat = OBJECT)
		{
			$this->tryConnect = true;
		
			if (!$this->connected)
			{
				$server = $this->source["server"];
				$login = $this->source["login"];
				$password = $this->source["password"];
				$dbname = $this->source["database"];
				
				$this->connection = @mysql_connect($server, $login, $password, true);
								
				if ($this->connection)
				{
					@mysql_select_db($dbname, $this->connection);
					
					mysql_query("SET NAMES 'utf8'");
				
					$this->connected = true;
					$this->outputFormat = $outputFormat;						
				}
				else
				{
					die('<b>Cannot connect to mySQL server <i>'.$server.'</i>:<br/>'.mysql_error().'</b>');
				}
			}
		}
		
		function execute($query)
		{
			if (!$this->connected && !$this->tryConnect)
			{
				$this->connect();
			}
		
			if ($this->connected)
			{			
				if (($result = mysql_query($query, $this->connection)) === false)
				{
					throw new Exception("SQL error: <b>$query</b>:<br /><br />".mysql_error());
					
					die();
					return null;
				}
				
				if (!is_null(EbApplication::$debug))
				{
					EbApplication::$debug->addQuery($query);
				}
				
				$this->rowAffected = 0;
				$this->insertId = 0;
								
				if (preg_match("/^\\s*(create|drop) /i",$query) ) 
				{
					return $result;
				}
				elseif ( preg_match("/^\\s*(truncate|insert|delete|update|replace) /i",$query) ) 
				{
					$this->rowAffected = mysql_affected_rows($this->connection);
				
					if ( preg_match("/^\\s*(insert|replace) /i",$query) ) 
					{
						return $this->insertId = mysql_insert_id($this->connection);
					}
					else
					{
						return $this->rowAffected;
					}
				} 
				else 
				{
					$num_rows = 0;
					$this->result = array();
					
					
									
					while ( $row = mysql_fetch_object($result) ) 
					{
						//var_dump($row);echo '<br /><br />';
						
						foreach ($row AS $key=>&$value)
						{
							if (is_string($value))
							{
								$value = stripslashes($value);
							}
						}
						
						$this->result[$num_rows] = $row;
						
						$num_rows++;
					}
															
					mysql_free_result($result);
										
					return $this->result;
				}
			}
			else
			{
				return false;
			}
		}
				
		public function __toString()
		{
			return "MySQLDataSource";
		}
	}